package views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import presenters.PlanChoosePresenter;


public class PlanChoosePanel extends JPanel {
	private PlanChoosePresenter myPresenter;
	private String planChoiceName; // set to current value of radio group
	private ButtonGroup bg;
	JButton planDetailsBtn = new JButton("Plan Details");
	private JRadioButton rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
	
	/**
	 * Create the panel.
	 */
	public PlanChoosePanel() {
		setLayout(new BorderLayout(0, 0));
		
		JLabel lblChooseADriving = new JLabel("Choose A Driving Plan");
		add(lblChooseADriving, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		
		Box horizontalBox = Box.createHorizontalBox();
		panel.add(horizontalBox);
		
		bg = new ButtonGroup();
		
		rdbtnNewRadioButton_2 = new JRadioButton("Daily Driving Plan");    // 1
		rdbtnNewRadioButton_1 = new JRadioButton("Frequent Driving Plan"); // 2
		rdbtnNewRadioButton = new JRadioButton("Occasional Driving Plan"); // 3
		bg.add(rdbtnNewRadioButton_2);
		bg.add(rdbtnNewRadioButton);
		bg.add(rdbtnNewRadioButton_1);
		horizontalBox.add(rdbtnNewRadioButton_2);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		horizontalBox.add(horizontalStrut);
		
		horizontalBox.add(rdbtnNewRadioButton_1);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		horizontalBox.add(horizontalStrut_1);
		
		horizontalBox.add(rdbtnNewRadioButton);
		planDetailsBtn.addActionListener(new MyActionListener());
		horizontalBox.add(planDetailsBtn);
		
		Component verticalStrut = Box.createVerticalStrut(100);
		add(verticalStrut, BorderLayout.SOUTH);

		RadioListener myListener = new RadioListener();
		rdbtnNewRadioButton.addActionListener(myListener);
		rdbtnNewRadioButton_1.addActionListener(myListener);
		rdbtnNewRadioButton_2.addActionListener(myListener);
		
		myPresenter = new PlanChoosePresenter(this);
	}
	
	public PlanChoosePresenter getMyPresenter() {
		return myPresenter;
	}
	
	public String getPlanChoice() {
		return planChoiceName;
	}
	
	public void setPlanChoice(int i) {
		switch(i) {
		case 1:
			rdbtnNewRadioButton_2.doClick();
			break;
		case 2:
			rdbtnNewRadioButton_1.doClick();
			break;
		case 3:
			rdbtnNewRadioButton.doClick();
			break;
		}
	}
	
	private class RadioListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Object source = arg0.getSource();
			
			if(source.equals(rdbtnNewRadioButton)) {
				planChoiceName = "Occasional Driving Plan";
			}
			else if(source.equals(rdbtnNewRadioButton_1)) {
				planChoiceName = "Frequent Driving Plan";
			}
			else if(source.equals(rdbtnNewRadioButton_2)) {
				planChoiceName = "Daily Driving Plan";
			}
		}
		
	}
	
	private class MyActionListener implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			JLabel lbl = new JLabel(new ImageIcon(GTCarRental.class.getResource("image.png")));
		    JOptionPane.showMessageDialog(null, lbl, "Plan Details", 
		                                 JOptionPane.PLAIN_MESSAGE, null);
		}
	}

}
